import { showToast, onPlayRecord } from "/utils/common";
import request from "/utils/request"
import { addWordRecord, getWordRecord } from "/api/clock/index"
import { bus } from "/utils/core";
const app = getApp()

Component({
  mixins: [],
  data: {
    type: 'all', // all WORDAGE VOICE
    visibleWordModal: false, // 显示添加文字记录modal
    visibleRecordManager: false, // 显示录音器
    activeFileUrl: null,
    list: []
  },
  props: {
    form: null
  },
  didMount() {
    bus.subscribe("queryWordRecordList", (params) => {
      this.setData({
        list: []
      })
      dd.showLoading({
        content: "加载中..."
      })
      request({
        url: getWordRecord,
        data: {
          ...params
        }
      }).then(res => {
        this.setData({
          list: res.voList ? res.voList : []
        })
      }).finally(() => {
        dd.hideLoading()
      })
    })
  },
  didUpdate() { },
  didUnmount() {
    bus.remove("queryWordRecordList")
  },
  methods: {
    // 显示or隐藏添加文字记录modal
    onWordModalShow(e) {
      this.setData({
        visibleWordModal: e.target.dataset.show
      })
    },
    // 添加拜访文字记录
    onWordRecordCreate(e) {
      if (!e.detail.value.textarea) {
        showToast({
          msg: '文字记录不能为空'
        })
        return
      }
      if (e.detail.value.textarea.length < 20) {
        showToast({
          msg: '文字记录字数需大于20'
        })
        return
      }
      dd.showLoading({
        content: '加载中...'
      })
      request({
        url: addWordRecord,
        method: 'post',
        headers: {
          "Content-Type": "application/json"
        },
        data: {
          content: e.detail.value.textarea,
          visitId: this.props.form.id,
          recordType: "WORDAGE"
        }
      }).then(res => {
        res.vo.user = {
          dingUserName: app.globalData.userInfo.userName
        }
        this.data.list.unshift(res.vo)
        this.setData({
          list: this.data.list
        })
        this.onWordModalShow({
          target: {
            dataset: {
              show: false
            }
          }
        })
      }).finally(() => {
        dd.hideLoading()
      })
    },
    // 显示录音
    onRecordManagerShow() {
      this.setData({
        visibleRecordManager: true
      })
    },
    // 隐藏录音
    onRecordHide() {
      this.setData({
        visibleRecordManager: false
      })
    },
    // 创建语音记录
    onRecordCreate(file) {
      dd.showLoading({
        content: "加载中..."
      })
      request({
        url: addWordRecord,
        method: 'post',
        headers: {
          "Content-Type": "application/json"
        },
        data: {
          recordType: "VOICE",
          sysEnclosureVO: file,
          visitId: this.props.form.id
        }
      }).then(res => {
        res.vo.user = {
          dingUserName: app.globalData.userInfo.userName
        }
        res.vo.sysEnclosureVO = file
        this.data.list.unshift(res.vo)
        this.setData({
          list: this.data.list,
          visibleRecordManager: false
        })
      }).finally(() => {
        dd.hideLoading()
      })
    },
    onPlayVoice(e) {
      if (this.data.activeFileUrl) {
        return
      }
      this.setData({
        activeFileUrl: e.target.dataset.url
      })
      onPlayRecord(e.target.dataset.url).finally(() => {
        this.setData({
          activeFileUrl: null
        })
      })
    },
    onSwitchType(e) {
      this.setData({
        type: e.target.dataset.type
      })
    }
  },
});
